package com.yj.springbootasync.task;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Random;
import java.util.concurrent.CompletableFuture;

@Component
@Slf4j
public class AsyncTask {

    public static Random random = new Random();

    @Async
    public CompletableFuture doTaskOne() throws Exception {
        log.info("开始做任务一");
        long startTime = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long endTime = System.currentTimeMillis();
        log.info("完成任务一，耗时："+(endTime - startTime) +"ms");
        return CompletableFuture.completedFuture("任务一完成！");
    }

    @Async
    public CompletableFuture doTaskTwo() throws Exception {
        log.info("开始做任务二");
        long startTime = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long endTime = System.currentTimeMillis();
        log.info("完成任务二，耗时："+(endTime - startTime) +"ms");
        return CompletableFuture.completedFuture("任务二完成！");
    }




}
